package com.jc.order.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jc.order.common.OrderNumberGenerator;
import com.jc.order.domain.EciCustomerSaleApply;
import com.jc.order.dto.EciCustomerSaleApplyDTO;
import com.jc.order.mapper.EciCustomerSaleApplyMapper;
import com.jc.order.service.EciCustomerSaleApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
@Slf4j
public class EciCustomerSaleApplyServiceImpl implements EciCustomerSaleApplyService {


    @Resource
    private EciCustomerSaleApplyMapper eciCustomerSaleApplyMapper;


    @Override
    public EciCustomerSaleApplyDTO insertDeclareData(EciCustomerSaleApplyDTO dto) {
        if (dto == null) {
            return null;
        }
        dto.setCorpSerialNo(OrderNumberGenerator.generate("DI"));
        EciCustomerSaleApply entity = new EciCustomerSaleApply();
        BeanUtils.copyProperties(dto, entity);
        eciCustomerSaleApplyMapper.insert(entity);
        dto.setId(entity.getId());
        return dto;

    }

    @Override
    public List<EciCustomerSaleApplyDTO> selectSaleApply(EciCustomerSaleApplyDTO dto) {

        List<EciCustomerSaleApplyDTO> dtoLit = null;
        Example example = new Example(EciCustomerSaleApply.class, Boolean.TRUE, Boolean.TRUE);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("tenantId", dto.getTenantId());
        Example.Criteria criteria2 = example.createCriteria();
        criteria2.orEqualTo("contractNo", dto.getContractNo());
        if (StrUtil.isNotEmpty(dto.getCorpSerialNo())) {
            criteria2.orEqualTo("corpSerialNo", dto.getCorpSerialNo());
        }
        example.and(criteria2);
        List<EciCustomerSaleApply> entityList = eciCustomerSaleApplyMapper.selectByExample(example);
        if (entityList != null && entityList.size() > 0) {
            dtoLit = new ArrayList<>();
            for (int i = 0; i < entityList.size(); i++) {
                EciCustomerSaleApply entity = entityList.get(i);
                EciCustomerSaleApplyDTO applyDTO = new EciCustomerSaleApplyDTO();
                BeanUtils.copyProperties(entity, applyDTO);
                dtoLit.add(applyDTO);
            }
        }
        return dtoLit;
    }

    @Override
    public EciCustomerSaleApplyDTO selectByCorpSerialNo(String corpSerialNo, Long tenantId) {
        EciCustomerSaleApplyDTO applyDTO = null;
        Example example = new Example(EciCustomerSaleApply.class, Boolean.TRUE, Boolean.TRUE);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("tenantId", tenantId);
        criteria.andEqualTo("corpSerialNo", corpSerialNo);
        EciCustomerSaleApply entity = eciCustomerSaleApplyMapper.selectOneByExample(example);
        if (entity != null) {
            applyDTO = new EciCustomerSaleApplyDTO();
            BeanUtils.copyProperties(entity, applyDTO);
        }
        return applyDTO;
    }

    @Override
    public Boolean updateCustomerSaleApply(EciCustomerSaleApplyDTO dto) {
        EciCustomerSaleApply entity = new EciCustomerSaleApply();
        BeanUtils.copyProperties(dto, entity);
        Example example = new Example(EciCustomerSaleApply.class, Boolean.TRUE, Boolean.TRUE);
        example.createCriteria().andEqualTo("tenantId", dto.getTenantId()).andEqualTo("corpSerialNo", dto.getCorpSerialNo());
        int row = eciCustomerSaleApplyMapper.updateByExample(entity, example);
        return row > 0;
    }


    @Override
    public List<EciCustomerSaleApplyDTO> getBySerialNoList(List<String> corpSerialNoList, Long tenantId) {
        if (CollectionUtils.isEmpty(corpSerialNoList))
            return Collections.emptyList();


        Example example = new Example(EciCustomerSaleApply.class);
        example.createCriteria()
                .andEqualTo("tenantId", tenantId)
                .andIn("corpSerialNo", corpSerialNoList);

        return toDTOs(eciCustomerSaleApplyMapper.selectByExample(example));
    }

    private List<EciCustomerSaleApplyDTO> toDTOs(List<EciCustomerSaleApply> entities) {
        if (CollectionUtils.isEmpty(entities))
            return Collections.emptyList();

        return entities.stream()
                .map(this::toDTO)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }


    private EciCustomerSaleApplyDTO toDTO(EciCustomerSaleApply entity) {
        if (entity == null) return null;
        EciCustomerSaleApplyDTO dto = new EciCustomerSaleApplyDTO();
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }
}
